/*
 * CodeViewer.java  0.4.0 / Oct 23, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.swingx.text.code;

import com.cute4j.swing.TextArea;
import com.cute4j.swing.events.SwingEvent;
import com.cute4j.swing.graphics.Color;
import com.cute4j.swing.graphics.Font;
import com.cute4j.swing.graphics.Graphics;
import com.cute4j.swing.text.TextHighlighter;

/**
 * Code Viewer.
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public class CodeViewer extends TextArea {

// Private Fields. -------------------------------------------------------------

  private int marginWidth;
  private int marginLocation;
  private Color marginColor;

// Public Constructors. --------------------------------------------------------

  /**
   * Constructs new CodeViewer object.
   */
  public CodeViewer() {
    this(80);
  }

  /**
   * Constructs new CodeViewer object.
   *
   * @param marginWidth the width of the margin
   */
  public CodeViewer(int marginWidth) {
    this.marginWidth = marginWidth;
    marginColor = Color.LIGHT_GRAY;
    if (marginWidth > 0) {
      enableEvents(SwingEvent.PAINT_VIEW_EVENT_MASK);
    }
    super.setEditable(false);
  }

  /**
   * Constructs new CodeViewer object.
   *
   * @param highlighter the document highlighter
   */
  public CodeViewer(TextHighlighter highlighter) {
    this(80);
    getDocument().setHighlighter(highlighter);
  }

// Public Methods. -------------------------------------------------------------

  /**
   * Returns the width of the margin.
   *
   * @return the width of the margin
   */
  public int getMarginWidth() {
    return marginWidth;
  }

  /**
   * Sets the width of the margin.
   *
   * @param marginWidth the width of the margin
   */
  public void setMarginWidth(int marginWidth) {
    if (this.marginWidth != marginWidth) {
      this.marginWidth = marginWidth;
      if (marginWidth > 0) {
        enableEvents(SwingEvent.PAINT_VIEW_EVENT_MASK);
      } else {
        disableEvents(SwingEvent.PAINT_VIEW_EVENT_MASK);
      }
    }
  }

  /**
   * Returns the color of the margin.
   *
   * @return the color of the margin
   */
  public Color getMarginColor() {
    return marginColor;
  }

  /**
   * Sets the color of the margin.
   *
   * @param marginColor the color of the margin
   */
  public void setMarginColor(Color marginColor) {
    this.marginColor = marginColor;
    update();
  }

// Overridden Methods. ---------------------------------------------------------

  /**
   * {@inheritDoc}
   */
  public void setFont(Font font) {
    super.setFont(font);
    marginLocation = 0;
  }

  /**
   * {@inheritDoc}
   */
  public void setEditable(boolean editable) {
    // forbidden method
  }

  /**
   * {@inheritDoc}
   */
  protected void paintComponentView(Graphics g, int x, int y, int width,
                                    int height) {
    if (marginLocation == 0) {
      marginLocation = (marginWidth *
                        g.getFontMetrics().stringWidth("M")) + 3;
    }
    g.setColor(marginColor);
    g.drawLine(marginLocation, y, marginLocation, y + height);
    super.paintComponentView(g, x, y, width, height);
  }

  /**
   * {@inheritDoc}
   */
  protected void processStyleChange() {
    super.processStyleChange();
    marginLocation = 0;
  }

}
